package com.the_last.controller;

import com.the_last.pojo.dto.VenueDto;
import com.the_last.pojo.vo.PageVO;
import com.the_last.pojo.vo.TimeSlotVO;
import com.the_last.pojo.vo.VenueTreeVO;
import com.the_last.pojo.vo.VenueVO;
import com.the_last.service.VenueService;
import com.the_last.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/venue")
public class VenueController {
    @Autowired
    private VenueService venueService;
    
    /**
     * 根据运动类型id获取场地列表
     *
     * @param sportTypeId 运动类型id
     * @return 场地列表
     */
    @GetMapping("/listBySportType")
    public Result<PageVO<VenueVO>> getVenuesBySportTypeId(
            @RequestParam(defaultValue = "0") Integer sportTypeId,
            @RequestParam(defaultValue = "-1") Integer status,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        return venueService.getVenuesBySportTypeId(sportTypeId, status, pageNum, pageSize);
    }
    
    @GetMapping("/timeslotsByVenueId")
    public Result<List<TimeSlotVO>> getTimeslotsByVenueId(@RequestParam Integer venueId, @RequestParam String date) {
        return venueService.getTimeslotsByVenueId(venueId, date);
    }
    
    /**
     * 更新场馆信息
     *
     * @param venueDto 场馆信息
     * @return 更新结果
     */
    @PutMapping("/update")
    @PreAuthorize("hasAuthority('VENUE_UPDATE')")
    public Result<String> updateVenue(@RequestBody VenueDto venueDto) {
        return venueService.updateVenue(venueDto);
    }
    
    /**
     * 新增场馆
     *
     * @param venueDto 场馆信息
     * @return 新增结果
     */
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('VENUE_CREATE')")
    public Result<Void> addVenue(@RequestBody VenueDto venueDto) {
        return venueService.addVenue(venueDto);
    }
    
    /**
     * 删除场馆
     *
     * @param id 场馆ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('VENUE_DELETE')")
    public Result<Void> deleteVenue(@PathVariable Integer id) {
        return venueService.deleteVenue(id);
    }
    
    /**
     * 获取场馆树形结构
     *
     * @return 场馆树形结构数据
     */
    @GetMapping("/tree")
    @PreAuthorize("hasAuthority('VENUE_QUERY')")
    public Result<List<VenueTreeVO>> getVenueTree() {
        return venueService.getVenueTree();
    }
}